package com.fessc.develop.utils;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class ObjectUtil {

    /**
     * 对象转化为Map
     *
     * @param object
     * @return
     */
    public static Map<String, Object> modelToMap(Object object) {
        Map<String, Object> map = new HashMap();
        for (Field field : object.getClass().getDeclaredFields()) {
            try {
                boolean flag = field.isAccessible();
                field.setAccessible(true);
                Object o = field.get(object);
                if (null != o) {
                    map.put(underScoreName(field.getName()), o);
                }
                field.setAccessible(flag);
            } catch (Exception e) {
                log.info("异常信息：" + e.getMessage());
            }
        }
        return map;
    }

    public static String underScoreName(String name) {
        StringBuilder result = new StringBuilder();
        if (name != null && !name.isEmpty()) {
            result.append(name.substring(0, 1).toUpperCase());
            for (int i = 1; i < name.length(); i++) {
                String s = name.substring(i, i + 1);
                if (s.equals(s.toUpperCase()) && !Character.isDigit(s.charAt(0))) {
                    result.append("_");
                }
                result.append(s.toUpperCase());
            }
        }

        return result.toString();
    }

    /**
     * 判断属性值是否全部为空
     *
     * @param object
     * @return
     */
    public static boolean isAllFieldNull(Object object) {
        boolean flag = true;
        Class clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.getName().equals("serialVersionUID")) continue; //忽略 serialVersionUID，类中 serialVersionUID = 1L;
            if (isAllUpperCase(field.getName()))
                continue; //忽略类中类似 USER_ID大写定义的属性 public static final String USER_ID = "user_id";
            //设置属性是可以访问的(私有的也可以)
            field.setAccessible(true);
            Object value = null;
            try {
                value = field.get(object);
                // 只要有1个属性不为空,那么就不是所有的属性值都为空
                if (value != null) {
                    flag = false;
                    break;
                }
            } catch (IllegalAccessException e) {
                log.info("异常信息：" + e.getMessage());
            }
        }
        //继承的父类中属性值也要判断
        Field[] superFields = clazz.getSuperclass().getDeclaredFields();
        for (Field superField : superFields) {
            if (superField.getName().equals("serialVersionUID")) continue;
            if (isAllUpperCase(superField.getName())) continue;
            //设置属性是可以访问的(私有的也可以)
            superField.setAccessible(true);
            Object value = null;
            try {
                value = superField.get(object);
                // 只要有1个属性不为空,那么就不是所有的属性值都为空
                if (value != null) {
                    flag = false;
                    break;
                }
            } catch (IllegalAccessException e) {
                log.info("异常信息：" + e.getMessage());
            }
        }
        return flag;
    }

    public static boolean isAllUpperCase(String fieldName) {
        boolean isUpper = true;
        fieldName = fieldName.replaceAll("[^(A-Za-z)]", "");
        for (int i = 0; i < fieldName.length(); i++) {
            char charAt = fieldName.substring(0, fieldName.length()).charAt(i);
            if (!Character.isUpperCase(charAt)) {
                isUpper = false;
                break;
            }
        }
        return isUpper;
    }


}
